package com.oem.fragment;

import static com.oem.fragment.contant.URLConstant.FILE_URL;
import static com.oem.fragment.contant.URLConstant.MINIO_URL;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.oem.fragment.config.RetrofitClient;
import com.oem.fragment.model.file.MediaFile;
import com.oem.fragment.model.file.PageResult;
import com.oem.fragment.service.FileService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeFragment extends Fragment {

    private ImageView imageView1, imageView2;
    private ImageView videoThumb1, videoThumb2;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home, container, false);

        // 初始化视图
        imageView1 = root.findViewById(R.id.image1);
        imageView2 = root.findViewById(R.id.image2);
        videoThumb1 = root.findViewById(R.id.video_thumb1);
        videoThumb2 = root.findViewById(R.id.video_thumb2);

        // 获取数据
        loadMediaFiles();

        return root;
    }

    // TODO 目前加载的时间稍长，后续考虑使用 Redis 缓存优化
    private void loadMediaFiles() {
        FileService api = RetrofitClient.getClient(FILE_URL).create(FileService.class);
        api.getMediaFiles(1, 4).enqueue(new Callback<PageResult<MediaFile>>() {
            @Override
            public void onResponse(Call<PageResult<MediaFile>> call, Response<PageResult<MediaFile>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    List<MediaFile> mediaList = response.body().getRecords();

                    int imageCount = 0;
                    int videoCount = 0;

                    for (MediaFile file : mediaList) {
                        String url = MINIO_URL + file.getBucket() + "/" + file.getFilePath();

                        if (file.isImage()) {
                            if (imageCount == 0) {
                                Glide.with(requireContext()).load(url).into(imageView1);
                            } else if (imageCount == 1) {
                                Glide.with(requireContext()).load(url).into(imageView2);
                            }
                            imageCount++;
                        } else if (file.isVideo()) {
                            if (videoCount == 0) {
                                Glide.with(requireContext())
                                        .load(url)
                                        .thumbnail(0.1f)
                                        .into(videoThumb1);

                            } else if (videoCount == 1) {
                                Glide.with(requireContext())
                                        .load(url)
                                        .thumbnail(0.1f)
                                        .into(videoThumb2);
                            }
                            videoCount++;
                        }
                    }
                } else {
                    Toast.makeText(requireContext(), "获取媒体资源失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<PageResult<MediaFile>> call, Throwable t) {
                Toast.makeText(requireContext(), "网络请求失败: " + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

}


